//index.js
//获取应用实例
const app = getApp();
const host = require('../../config');
console.log(app.globalData.unionid)
Page({
  data: {
    userInfo: {},
    hasUserInfo: false,
    list:[],
    shareTofresh:1,
    link:"",
    canIUse: wx.canIUse('button.open-type.getUserInfo')
  },
  onShareAppMessage(options) {
    var url=encodeURIComponent(options.webViewUrl);
    var that=this;
    var index=options.webViewUrl.indexOf("detail.html?id=");   
    return {
      title: '心猜',
      path:"/pages/index/index?url="+url,
      success: function(res) {
        console.log(options)
        if(index!=-1){
          var id=that.GetQueryString(options.webViewUrl,"id");
          that.getList(id);
        }
        
          
      },
      fail: function(res) {
        // 转发失败
      }
    }
    
    
    
  },
  onLoad: function (options) {
           console.log(options)
          app.userInfoReadyCallback = res => {
            
            if(options.url){
              var u=decodeURIComponent(options.url);
              var index=decodeURIComponent(options.url).indexOf("rid");
              if(index!=-1){
                var str1=this.changeUrlArg(u, 'token', app.globalData.token);
                var t1=this.changeUrlArg(str1, 'unionid',app.globalData.unionid);
                console.log(t1)
                var turl=t1.replace(/quiz_result/, "share");
                console.log(turl)
                this.setData({
                   link: turl
                });

              }else{
                console.log(app.globalData.token)
                var str=this.changeUrlArg(u, 'token', app.globalData.token);
                var t=this.changeUrlArg(str, 'unionid',app.globalData.unionid);
                 console.log(t)
                this.setData({
                  link: t
                });
              }
              
            }else{

                this.setData({
                   link: "https://xincai.xinduiapp.com/stx/xincai/index.html?token="+app.globalData.token+"&unionid="+app.globalData.unionid
                });
            }
            
          }
      
  },
  onShow: function () {
     
  },
  GetQueryString1:function(name){
     var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
     var r = this.data.link.substr(1).match(reg);
     if(r!=null)return  unescape(r[2]); return null;
  },
  changeUrlArg:function(url,arg,arg_val){
        var pattern=arg+'=([^&]*)'; 
        var replaceText=arg+'='+arg_val; 
        if(url.match(pattern)){ 
            var tmp='('+ arg+'=)([^&]*)'; 
            tmp=url.replace(new RegExp(tmp,'gi'),replaceText); 
            return tmp; 
        }else{ 
            if(url.match('[\?]')){ 
                return url+'&'+replaceText; 
            }else{ 
                return url+'?'+replaceText; 
            } 
        } 
        return url+'\n'+arg+'\n'+arg_val; 
  },
  GetQueryString:function(str,name){
     var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
     var index=str.indexOf("?");
     var search=str.slice(index)
     var r = search.substr(1).match(reg);
     if(r!=null)return  unescape(r[2]); return null;
  },
  goDetail:function(e){
    var id=e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../detail/detail?id='+id
    });
  },
  getCount:function(id){
      var that=this;
      wx.request({
          url: host+"/share_count",
          method:"POST",
          header: {
              'content-type': 'application/x-www-form-urlencoded' // 默认值
          },
          data: {
              q_id:id,
              unionid:app.globalData.unionid,
              route:"share_detail/"+id
          },
          success: function(res) {
           console.log(res);
          },
          fail: function(res) {
              
          }
      }); 
  },
  getList:function(id){
      var that=this;
      wx.request({
          url: host+"/get_question",
          method:"POST",
          header: {
              'content-type': 'application/x-www-form-urlencoded' // 默认值
          },
          data: {
              q_id:id,
              unionid:app.globalData.unionid
          },
          success: function(res) {
            if(res.code==1){
              that.setData({
                 shareTofresh:res.data.question.shareTofresh
              }); 
              if(that.data.shareTofresh==1) {
                 that.getCount(id)
              }
            }
           
          },
          fail: function(res) {
              
          }
      }); 

  }


})
